<?php

class RichtextField extends Object
{
    var $Type = 'RichtextField';
    
    var $_Name;
    var $_Title;
    var $_Description;
    var $_Value;
    var $_Regex;
    var $_Mode;

    function __construct(&$form, $name, $title, $description = '', $value = '', $required = false, $regex = '')
    {
        $this->_Name        = $name;
        $this->_Title       = $title;
        $this->_Description = $description;
        $this->_Value       = $value;
        $this->_Mode        = $required?'required':'normal';
        $this->_Regex       = $regex;
    }
        
    function Validate()
    {
        if ($this->_Mode == 'required')
        {
            // Ensure some text was entered
            if (!isset($_POST[$this->_Name]) || (strlen($_POST[$this->_Name]) == 0))
            {
                $this->_Mode  = 'error';
                $this->_Value = '';

                return false;
            }

            // If specified validate value against the given regex
            if (strlen($this->_Regex) > 0)
            {
                if (preg_match($this->_Regex, $_POST[$this->_Name]) !== 1)
                {
                    $this->_Mode  = 'error';
                    $this->_Value = '';

                    return false;
                }
            }

            // Field is valid
            $this->_Value = $_POST[$this->_Name];
            return true;

        }
        else
        {
            $this->_Value = $_POST[$this->_Name];
            return true;
        }
    }

    function ToArray()
    {
        return array('type'        => 'RichtextField',
                     'name'        => $this->_Name,
                     'title'       => $this->_Title,
                     'description' => $this->_Description,
                     'mode'        => $this->_Mode,
                     'value'       => $this->_Value);
    }
}

?>